package com.gsm.service.impl;

import com.gsm.dao.CourseDao;
import com.gsm.entity.Result;
import com.gsm.po.Course;
import com.gsm.service.CourseService;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 课程(Course)表服务实现类
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result insertCourse(Course course) {
        Integer showOrder = courseDao.selectMaxShowOrderByChapterId(course.getChapterId());
        long courseId = idUtils.nextId();
        course.setCourseId(courseId);
        course.setShowOrder(showOrder == null ? 0 : ++showOrder);
        course.setCourseView(0);
        course.setCourseSize(0);
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        courseDao.insertCourse(course);
        return Result.success();
    }

    /**
     * 根据 主键 courseId 删除
     *
     * @param courseId 主键
     * @return
     */
    @Override
    public Result deleteCourseByCourseId(Long courseId) {
        courseDao.deleteCourseByCourseId(courseId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param course 实例对象
     * @return
     */
    @Override
    public Result updateCourseByCourseId(Course course) {
        course.setUpdateTime(new Date());
        courseDao.updateCourseByCourseId(course);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param courseId 主键
     * @return
     */
    @Override
    public Result selectCourseByCourseId(Long courseId) {
        Course course = courseDao.selectCourseByCourseId(courseId);
        Result result = Result.success(course);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param course 实例对象
     * @return
     */
    @Override
    public Result selectCourseList(Course course) {
        List<Course> list = courseDao.selectCourseList(course);
        Result result = Result.success(list);
        return result;
    }

    @Override
    public Result selectCourseListByChapterId(Long chapterId) {
        List<Course> list = courseDao.selectCourseListByChapterId(chapterId);
        Result result = Result.success(list);
        return result;
    }

    @Override
    public Result updateCourseShowOrder(Long sid, Long cid, List<Long> ids) {
        courseDao.updateCourseShowOrder(ids);
        return Result.success();
    }
}